package com.tomcat.server;

import com.tomcat.mapper.Mapper;
import com.tomcat.mapper.MapperData;
import com.tomcat.mapper.Wrapper;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.Socket;

/**
 * desc:
 *
 * @author tengfei wang
 * @version 1.0
 * @date Created in 25/9/2020
 */
public class RequestProcessor extends Thread {

    private final Socket socket;

    private final Mapper mapper;

    private final String appBase;

    public RequestProcessor(Socket socket, Mapper mapper, String appBase) {
        this.socket = socket;
        this.mapper = mapper;
        this.appBase = appBase;
    }

    @Override
    public void run() {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            // 封装 request 和 response
            inputStream = socket.getInputStream();
            Request request = new Request(inputStream);
            outputStream = socket.getOutputStream();
            Response response = new Response(outputStream, appBase + request.getContextPath());

            String servletPath = request.getServletPath();
            String contextPath = request.getContextPath();
            // 如果执行的是mini tomcat本身，跳转到自身
            if ("".equalsIgnoreCase(contextPath.trim())) {
                if (servletPath == null || "".equals(servletPath)) {
                    response.sendRedirect(this.getClass().getResource("/").getPath(), "index.html");
                } else {
                    response.sendRedirect(this.getClass().getResource("/").getPath(), "");
                }
                return;
            }

            // 从mapper中获取当前请求的MapperData数据
            MapperData mapperData = mapper.getMapperData(request.getServiceName(), contextPath, servletPath);
            Wrapper wrapper = mapperData.getWrapper();
            // 判断wrapper是否为空，如果为空可能执行的是静态文件
            if (wrapper == null) {
                System.out.println("执行静态文件--> " + request.getRequestURI());
                response.getHtml(appBase + request.getRequestURI());
            } else {
                Object httpServlet = wrapper.getServlet();
                System.out.println("执行Servlet-->" + httpServlet.getClass().getName());
                Class<?> servletClass = httpServlet.getClass();
                Method service = servletClass.getMethod("service", ServletRequest.class, ServletResponse.class);
                service.invoke(httpServlet, request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
